#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Matrix;

Matrix operator+(const Matrix &m1, const Matrix &m2);
Matrix operator-(const Matrix &m1, const Matrix &m2);
Matrix operator*(const Matrix &m1, const Matrix &m2);
/********* Begin *********/
class Matrix
{
    // 矩阵类的声明
private:
    int rows;
    int cols;
    vector<vector<int>> data;

public:
    Matrix(int r, int c);
    void Fill(int value);
    void Set(int r, int c, int value);
    int Get(int r, int c);
    void Print();
    friend Matrix operator+(const Matrix &m1, const Matrix &m2);
    friend Matrix operator-(const Matrix &m1, const Matrix &m2);
    friend Matrix operator*(const Matrix &m1, const Matrix &m2);
};
//矩阵类的定义

Matrix::Matrix(int r, int c)
    : rows(r), cols(c), data(r, vector<int>(c, 0))
{
}

void Matrix::Fill(int value)
{
    for (int i = 0; i < rows; ++i)
    {
        for (int j = 0; j < cols; ++j)
        {
            data[i][j] = value;
        }
    }
}

void Matrix::Set(int r, int c, int value)
{
    data[r][c] = value;
}

int Matrix::Get(int r, int c)
{
    return data[r][c];
}

void Matrix::Print()
{
    for (int i = 0; i < rows; ++i)
    {
        for (int j = 0; j < cols; ++j)
        {
            cout << data[i][j] << "_";
        }
        cout << endl;
    }
}

Matrix operator+(const Matrix &m1, const Matrix &m2)
{
    // 实现矩阵加法
    if (m1.rows != m2.rows || m1.cols != m2.cols)
    {
        cerr << "Error: Matrices must be of the same size for subtraction." << endl;
        exit(1); // 或者返回一个特殊的错误矩阵
    }
    Matrix m(m1.rows, m1.cols);
    for (int i = 0; i < m1.rows; ++i)
    {
        for (int j = 0; j < m1.cols; ++j)
        {
            m.data[i][j] = m1.data[i][j] + m2.data[i][j];
        }
    }
    return m;
}

Matrix operator-(const Matrix &m1, const Matrix &m2)
{
    // 实现矩阵减法
    if (m1.rows != m2.rows || m1.cols != m2.cols)
    {
        cerr << "Error: Matrices must be of the same size for subtraction." << endl;
        exit(1); // 或者返回一个特殊的错误矩阵
    }
    Matrix m(m1.rows, m1.cols);
    for (int i = 0; i < m1.rows; ++i)
    {
        for (int j = 0; j < m1.cols; ++j)
        {
            m.data[i][j] = m1.data[i][j] - m2.data[i][j];
        }
    }
    return m;
}

Matrix operator*(const Matrix &m1, const Matrix &m2)
{
    // 实现矩阵乘法
    if (m1.rows != m2.cols || m1.cols != m2.rows)
    {
        cerr << "Error: Matrices must be of the same size for subtraction." << endl;
        exit(1); // 或者返回一个特殊的错误矩阵
    }
    Matrix m(m1.rows, m2.cols);
    for (int i = 0; i < m1.rows; ++i)
    {
        for (int j = 0; j < m1.cols; ++j)
        {

            //////////////////////////////|冲突
            for (int k = 0; k < m1.cols; ++i)
            {
                m.data[i][j] += m1.data[i][k] * m2.data[k][j];
            }
        }
    }
    return m;
}
/********* End *********/


int main()
{
    Matrix m1(3,3),m2(3,3);
    m1.Fill(1);
    m2.Fill(10);
    Matrix ret(3,3);
    ret = m1+m2;
    ret.Print();
    return 0;
}